package com.tjw.ec.upms.controller;

import com.tjw.ec.common.core.util.PageUtils;
import com.tjw.ec.common.core.util.R;
import com.tjw.ec.upms.api.dto.RoleVO;
import com.tjw.ec.upms.api.entity.RoleEntity;
import com.tjw.ec.upms.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 控制动作层： 角色表,基本权限的集合。无上级与下级之分;
 *
 * @author T-junwen
 * @email t-junwen@foxmail.com
 * @date 2019-07-27 17:28:58
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * @Title: 根据用户id查询用户角色
     * @Param: []
     * @return: com.tjw.ec.common.core.util.Response<java.util.List < com.tjw.ec.upms.api.dto.RoleVO>>
     * @Author: 田俊文
     * @Date: 2019/8/2 14:08
     * @Throws:
     */
    @GetMapping("/getRoleByUserId/{userId}")
    public R getRoleByUserId(@PathVariable("userId") String userId) {
        List<RoleVO> roles = roleService.getRoleByUserId(userId);
        return R.data(roles);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = roleService.queryPage(params);
        return R.data(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{roleId}")
    public R info(@PathVariable("roleId") String roleId) {
        RoleEntity role = roleService.getById(roleId);
        return R.data(role);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody RoleEntity role) {
        roleService.save(role);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody RoleEntity role) {
        roleService.updateById(role);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestParam String[] ids) {
        roleService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
